<template>
  <div>
    <a-modal
      ok-text="确认"
      cancel-text="取消"
      :maskClosable="false"
      :title="title + '备料单'"
      :width="modalWidth"
      @cancel="handleClose"
      :destroyOnClose="true"
      @ok="handleOk"
      :visible="visible"
      :confirmLoading="confirmLoading"
      footer=""
    >
      <template>
        <div class="modalBut">
          <a-button @click="handleClose">
            取消
          </a-button>
          <a-button @click="handleOk" type="primary">确认</a-button>
        </div>
      </template>
      <a-spin :spinning="confirmLoading">
        <a-row>
          <a-col>
            <a-select
              v-model.trim="model.goodsId"
              placeholder="请选择计量单位"
              style="width:200px"
              @change="handleChange"
            >
              <a-select-option
                v-for="item in planMaterialList"
                :key="item.goodsId"
                :value="item.goodsId"
              >
                {{ item.goodsName }}
              </a-select-option>
            </a-select>
          </a-col>
        </a-row>
        <br />

        <a-table
          :columns="columns"
          :data-source="tableData"
          :loading="loading"
          :pagination="false"
          :rowKey="(record, index) => record.shelfGoodsNumId"
          :row-selection="{
            selectedRowKeys: selectedRowKeys,
            onChange: onSelectChange,
          }"
        >
        </a-table>
      </a-spin>
    </a-modal>
  </div>
</template>

<script>
import api from "@/api/apiServer";
import { tableMinxins } from "@/unit/mixins/tableMinxins.js";
import { wareHouseColumns } from "./tableConfig";
export default {
  mixins: [tableMinxins],
  data() {
    return {
      title: "", // 标题
      visible: false, // 状态、
      modalWidth: 1000, // 弹框宽度
      model: {
        goodsId: "",
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 8 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      planMaterialList: [],
      columns: wareHouseColumns,
      selectedRows: [],
    };
  },
  computed: {
    rules() {
      return {
        quantity: [
          { required: true, message: "请输入数量", trigger: "change" },
        ],
      };
    },
  },
  methods: {
    // 新增
    modalAdd() {
      this.modalEdit(this.model);
    },

    // 编辑
    modalEdit(planMaterialList) {
      this.planMaterialList = planMaterialList;
      this.$set(this.model, "goodsId", planMaterialList[0].goodsId);
      this.getSelShelfGoodsNum(planMaterialList[0].goodsId);
      this.resetScreenSize();
      this.visible = true;
    },
    handleChange(goodsId) {
      this.getSelShelfGoodsNum(goodsId);
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys;
      this.selectedRows = selectedRows;
    },
    async getSelShelfGoodsNum(goodsId) {
      const res = await api.SelShelfGoodsNum({ goodsId });
      this.tableData = res.data;
      this.tableData.forEach((item) => {
        this.$set(item, "materialPrepareCount", 0);
        this.$set(item, "shelfGoodsNumId", item.id);
        delete item.id;
      });
      console.log(this.tableData);
    },
    // 关闭
    handleClose() {
      this.visible = false;
      this.tableData = [];
      this.selectedRows = [];
      this.selectedRowKeys = [];
      this.model = Object.assign({});
    },
    handleOk() {
      // 保存时候返回的list
      this.$emit("ok", this.selectedRows);
      this.visible = false;
      this.tableData = [];
      this.selectedRows = [];
      this.selectedRowKeys = [];
      this.model = Object.assign({});
    },
    // 根据屏幕变化,设置抽屉尺寸
    resetScreenSize() {
      let screenWidth = document.body.clientWidth;
      if (screenWidth < 500) {
        this.modalWidth = screenWidth;
      } else {
        this.modalWidth = 1000;
      }
    },
  },
};
</script>


